$("#head").load("header.html",function () {})
$("#foot").load("footer.html",function () {})
new Vue({
    el:'#news',
    data:{
        news_list:[],//新闻列表
        block : [],
        arr:[],
        choose:'choose',
        news_list3:[],
        share_img :"",
        Newest:0
    },
    ready:function(){
        var self = this;
        //搜索
        $(document).keyup(function(event) {
            if (event.keyCode == 13) {
                if($("#search_val").val()==""){
                    alert("请输入要搜索的内容")
                }else {
                    $(location).attr('href', 'search.html?keyword='+$("#search_val").val());
                }
            }
        })
        $("span.serach_cl").click(function(){
            if($("#search_val").val()==""){
                alert("请输入要搜索的内容")
            }else {
                $(location).attr('href', 'search.html?keyword='+$("#search_val").val());
            }
        });
        //咨询列表
        self.getNewsList();
        var scr = function () {
            var winPos = $(window).scrollTop();
            if(winPos >= 400){
                $('#totop').css({'display':'inline-block'})
            }else{
                $('#totop').css({'display':'none'})
            }
        }
        $(window).scroll(function(){
            scr();
        })
        scr();
        $("#wx_icon").hover(function(){
            $("#wx_ii").show()
        },function(){
            $("#wx_ii").hide()
        });
        setInterval(function () {
            $.ajax({
                    url :"http://api.456mobi.com/api/?s=home/Information/get_newsflash_count",
                    type : 'post',
                    data:{
                        id : self.news_list[0].news_list2[0].id
                    },
                    success: function(res){
                        if(res.data>0){
                            self.Newest = res.data
                            $('#Newest').show()
                        }
                    }
                })
        },60000)
        $('#Newest').click(function () {
            location.reload()
        })
    },
    methods:{
        down : function(id,e){
            if($("#"+id).height()<88){
                $("#" + id).css({'maxHeight':'none'});
                $('#' + e.target.id).css({"transform":"rotate(0)"})
            }
            if($("#" + id).css('maxHeight') == '88px'){
                $("#" + id).css({'-webkit-line-clamp':'20'})
                $("#" + id).css({'maxHeight':'500px'})
                $('#' + e.target.id).css({"transform":"rotate(180deg)"})
            }else{
                $("#" + id).css({'-webkit-line-clamp':'3'})
                $("#" + id).css({'maxHeight':'88px'})
                $('#' + e.target.id).css({"transform":"rotate(0deg)"})
            }


        },
        getNewsList : function() {
            var self = this;
            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/newsflash_list",
                type : 'post',
                success: function(ret){
                    var arr = ret.data;
                    $.each(arr, function(index,val) {
                        var data = {};
                        var timestamp = Date.parse(index);
                        data.issue_time = self.timestampToTime_year(timestamp/1000);
                        data.date_str = self.timestampToTime_is(index);
                        self.arr.push(index);
                        var length=self.arr.length;
                        if(self.arr[length-2]!=index){
                            $(".news_list_box span.append").append("<p>"+index+"</p>");
                        }
                        data.week = self.timestampToTime_week(timestamp/1000);
                        self.news_list.push(data);
                        Vue.set(self.news_list[0],'news_list2',[])
                        self.news_list.forEach(function (item,index) {
                            $.each(val, function(index2,val2) {
                                var data2 = {}
                                data2.id = val2.id;
                                data2.title = val2.title;
                                data2.content = val2.content;
                                data2.source = val2.source;
                                data2.issue_time = val2.issue_time;
                                data2.bad_vote = val2.bad_vote;
                                data2.bull_vote = val2.bull_vote;
                                data2.evaluate = val2.evaluate;
                                item.news_list2.push(data2);
                            })

                        })

                    });


                }
            })
        },
        timestampToTime_year :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            return Y+M+D;
        },
        timestampToTime_fs :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + '';
            s = date.getSeconds();
            return h+m;
        },
        timestampToTime_is:function (timestamp) {
            var self = this;
            var now = new Date();
            var time = new Date(new Date(timestamp));
            D = now.getDate();
            D2 = time.getDate();
            if(D==D2){
                return "今天"
            }else if(D>D2){
                if(D-D2>2){
                    return ""
                }
                return "昨天"
            }

        },
        timestampToTime_week :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            W= date.getDay();
            if(W==0)  W="星期日"
            if(W==1)  W="星期一"
            if(W==2)  W="星期二"
            if(W==3)  W="星期三"
            if(W==4)  W="星期四"
            if(W==5)  W="星期五"
            if(W==6)  W="星期六"
            return W;
        },

        bull:function (id) {
            var self= this
            var count= parseInt($("#bull"+id+" span").html());
            // count+=1;
            // $("#bull"+id+" span").html(count);

            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    id : id,
                    sign:'bull_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bull_vote);
                    var bad = parseInt(ret.data.bad_vote);
                    $("#bull"+id+" span").html(count);
                    $("#bad"+id+" span").html(bad);

                    $("#bad"+id+" .li_kong1").hide();
                    $("#bad"+id+" .li_kong4").hide();
                    $("#bad"+id+" .li_kong2").hide();
                    $("#bad"+id+" .li_kong3").show();

                    if(ret.data.user_data.bull_vote == 1){
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao3").hide();
                        $("#bull"+id+" .li_hao4").show();
                    }else{
                        $("#bull"+id+" .li_hao4").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao3").show();
                    }
                }
            })
        },
        bad:function (id) {
            var count = parseInt($("#bad"+id+" span").html());
            // count+=1;
            // $("#bad"+id+" span").html(count);
            $("#bad"+id+" .li_kong2").show();
            $("#bad"+id+" .li_kong1").hide();
            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    id : id,
                    sign:'bad_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bad_vote);
                    var bull = parseInt(ret.data.bull_vote)
                    $("#bad"+id+" span").html(count);
                    $("#bull"+id+" span").html(bull);
                    $("#bull"+id+" .li_hao1").hide();
                    $("#bull"+id+" .li_hao4").hide();
                    $("#bull"+id+" .li_hao2").hide();
                    $("#bull"+id+" .li_hao3").show();

                    if(ret.data.user_data.bad_vote == 1){
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong3").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong4").show();
                    }else{
                        $("#bad"+id+" .li_kong4").hide();
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong3").show();
                    }
                }
            })
        },
        more:function () {
            var self = this;
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/newsflash_list',
                type:'post',
                data:{
                    id :self.news_list[self.news_list.length-1].news_list2[self.news_list[self.news_list.length-1].news_list2.length-1].id
                },
                success: function(ret){
                    var arr = ret.data;
                    $.each(arr, function(index,val) {
                        var data = {};
                        var timestamp = Date.parse(index);
                        data.issue_time = self.timestampToTime_year(timestamp/1000);
                        data.date_str = self.timestampToTime_is(index);
                        data.week = self.timestampToTime_week(timestamp/1000);
                        if(self.news_list[self.news_list.length-1].issue_time!=data.issue_time){
                            // alert(self.news_list[self.news_list.length-1].issue_time+"//"+data.issue_time)
                            self.news_list.push(data)
                            Vue.set(self.news_list[self.news_list.length-1],'news_list2',[])
                        }
                        $.each(val, function(index2,val2) {
                            var data2 = {}
                            data2.id = val2.id;
                            data2.title = val2.title;
                            data2.content = val2.content;
                            data2.source = val2.source;
                            data2.issue_time = val2.issue_time;
                            data2.bad_vote = val2.bad_vote;
                            data2.bull_vote = val2.bull_vote;
                            data2.evaluate = val2.evaluate;
                            // self.news_list[self.news_list.length-1].news_list2.push(data2);
                            Vue.set(self.news_list[self.news_list.length-1].news_list2,self.news_list[self.news_list.length-1].news_list2.length,data2)
                        })
                    });
                }
            })
        },
        enter: function(id){
            //修改值
            $("#bg_"+id).css("background","#fffbf4").find(".news_box_time_line span").eq(0).css({"border":"1px solid #f26d1c","background":"#f26d1c"});
            $("#bg_"+id).find(".left1_img").hide();
            $("#bg_"+id).find(".left2_img").show();
            $("#bg_"+id+" .news_box_time_line span").eq(2).css({"color":"#fff","background":"#f26d1c"});
            $("#bg_"+id+" .news_box_time_line span").eq(3).css({"border-top":"1px dotted #f26d1c"});
        },
        leave: function(id){
            //默认值
            $("#bg_"+id).css("background","#fff").find(".news_box_time_line span").eq(0).css({"border":"1px solid #b0b0b0","background":"#fff"});
            $("#bg_"+id).find(".left2_img").hide();
            $("#bg_"+id).find(".left1_img").show();
            $("#bg_"+id+" .news_box_time_line span").eq(2).css({"color":"#000","background":"#dee0e7"});
            $("#bg_"+id+" .news_box_time_line span").eq(3).css({"border-top":"1px dotted #bfbfbf"});
        },
        share:function (id,title,content) {
            var self = this;
          // if($("#share_"+id).css("display")!="block"){
               $("#share_"+id).show().parents(".choose_box").siblings().find(".bdsharebuttonbox").hide();
               var bdText = "【"+title+"】"+content;
               var bdPic = "http://api.456mobi.com/api/upload/newsflash/"+id+".jpg";
               window._bd_share_config={
                   "common":{
                       "bdSnsKey":{},
                       "bdText":bdText,
                       "bdMini":"2",
                       "bdPic":"",
                       "bdStyle":"0",
                       "bdSize":"16"
                   },
                   "share":{},
                   "image":{
                       "viewList":[
                           "tsina",
                           "sqq",
                           "weixin"
                       ],
                       "viewText":"分享到：",
                       "viewSize":"16"
                   },
                   "selectShare":{
                       "bdContainerClass":null,
                       "bdSelectMiniList":[
                           "tsina",
                           "sqq",
                           "weixin"
                       ]},
               };
               with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+ ~(-new Date() / 36e5)];
               window._bd_share_main.init();
           //}
            //else {
              // $("#share_"+id).hide();
          // }

        },
        share_out:function (id) {
           // $("#share_"+id).hide()
            $(".bdsharebuttonbox").hide();
        },
        wx_share:function (id) {
            var self = this;
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/get_share_pic',
                type:'post',
                data:{
                    id :id
                },
                success: function(ret){
                    // console.log(JSON.stringify(ret));
                    if(ret.code==1){
                        $("#share_img_"+id).show();
                        $("#share_img_"+id).html("<img src='"+ret.data+"' />");
                    }else{
                        alert("获取失败")
                    }
                }
            })
        },
    }
});